#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    int pipefd[2];
    if (pipe(pipefd) < 0)
    {
        perror("pipe fail");
        return -1;
    }
    char buffer[256];
    int pid = fork();
    if (pid < 0)
    {
        perror("fork fail");
        return -1;
    }
    if (pid == 0)
    {
        // child process
        // 关闭写端
        close(pipefd[1]);
        sleep(1);
        if (read(pipefd[0], buffer, sizeof(buffer)))
        {
            printf("child recieve : %s\n", buffer);
        }
        
        //进程退出，关闭读端
        close(pipefd[0]);
        printf("child quit");
    }
    else
    {
        // 关闭读端
        close(pipefd[0]);
        char str[] = "i am father";
        strcpy(buffer, str);
        write(pipefd[1], buffer, sizeof(buffer));
        printf("父进程写入\n");

        //进程退出，关闭写端
        close(pipefd[1]);
        wait(NULL);
    }
    return 0;
}